package com.handersome.db.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.handersome.db.entity.SchoolEntity;
import com.handersome.db.repository.SchoolRepository;
import com.handersome.type.EhCacheType;
import com.handersome.util.EhcacheUtil;

@Service
public class SchoolService {
	
	private Logger logger = LoggerFactory.getLogger(SchoolService.class);

	@Autowired
	private SchoolRepository schoolRpo;
	
	public List<SchoolEntity> getSchoolByNameAndAddress(String name, String address) {
		List<SchoolEntity> list = schoolRpo.findByNameAndAddress(name, address);
		logger.info(JSON.toJSONString(list));
		return list;
	}
	
	public List<SchoolEntity> findAll() {
		String key = "school_list";
		List<SchoolEntity> list;
		if (EhcacheUtil.getInstance().isExist(key, EhCacheType.USER_CACHE.getCacheName())) {
			list = (List<SchoolEntity>) EhcacheUtil.getInstance().get(EhCacheType.USER_CACHE.getCacheName(), key);
		} else {
			list = schoolRpo.findAll();
			EhcacheUtil.getInstance().put(EhCacheType.USER_CACHE.getCacheName(), key, list, 2);//设置2秒后过期
		}
		logger.info(JSON.toJSONString(list));
		return list;
	}
}
